//
//  SettingCell.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2022/2/7.
//

import Adaptor
import App_Common_UIKitExtensions
import App_Common_Views
import Chain
import Foundation
import UICore
import UIKit

class SettingCell: CollectionViewCell {
    lazy var titleLabel: UILabel = .init()
        .x
        .textColor(Adaptor.Profile.Setting.titleTextColor)
        .font(.root)
        .instance

    lazy var detailLabel: UILabel = .init()
        .x
        .textColor(Adaptor.Profile.Setting.detailTextColor)
        .font(.root)
        .instance

    lazy var switchView: UISwitch = .init()
        .x
        .isHidden(true)
        .instance

    lazy var iconView: UIImageView = .init()
        .x
        .instance

    lazy var indicatorImageView: UIImageView = .init()
        .x
        .image(ProfileModular.image(named: "Common.Cell.Indicator")?.withTintColor(Adaptor.Profile.Setting.detailTextColor))
        .instance

    lazy var lineView: UIView = LineView(frame: .zero, lineColor: Adaptor.Profile.lineBorderColor, lineWidth: 1.0)
        .x
        .instance

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") }
}

extension SettingCell {
    func setupUI() {
        titleLabel.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalToSuperview().offset(20)
            $0.centerY.equalToSuperview()
        }

        iconView.x.add(to: contentView).snp.makeConstraints {
            $0.centerY.equalTo(titleLabel)
            $0.leading.equalTo(titleLabel.snp.trailing).offset(10)
        }

        indicatorImageView.x.add(to: contentView).snp.makeConstraints {
            $0.trailing.equalToSuperview().offset(-20)
            $0.centerY.equalToSuperview()
        }

        detailLabel.x.add(to: contentView).snp.makeConstraints {
            $0.trailing.equalTo(indicatorImageView.snp.leading).offset(-14)
            $0.centerY.equalToSuperview()
            $0.leading.greaterThanOrEqualTo(titleLabel.snp.trailing).offset(10).priority(400)
        }

        switchView.x.add(to: contentView).snp.makeConstraints {
            $0.trailing.equalToSuperview().offset(-20)
            $0.centerY.equalToSuperview()
        }

        lineView.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalToSuperview()
            $0.trailing.equalToSuperview()
            $0.height.equalTo(1)
            $0.bottom.equalToSuperview()
        }
    }
}

extension SettingCell: SimpleCell {
    static func estimatedItemSize(viewModel _: SimpleCellViewModel?) -> CGSize {
        CGSize(width: Adaptor.Screen.width, height: 56)
    }

    func reusableCell(_ viewModel: SimpleCellViewModel) {
        guard let settingViewModel = viewModel as? SettingCellViewModel else { return }
        let data = settingViewModel.output
        titleLabel.text = data.title
        detailLabel.text = data.detail
        iconView.image = data.icon
        indicatorImageView.isHidden = !data.showindicator
        switchView.isHidden = !data.showSwitch
        switchView.isOn = data.isOn
    }
}
